package com.example.test622.detail

import android.widget.Toast
import androidx.fragment.app.Fragment
import com.example.common.CommonAdapter
import com.example.common.mvi.BaseFragment
import com.example.common.mvi.BaseViewModel
import com.example.net.entity.CommentEntity
import com.example.net.entity.SimpleVideoEntity
import com.example.test622.BR
import com.example.test622.R
import com.example.test622.databinding.FragmentDetailCommentBinding
import com.tencent.mmkv.MMKV

class DetailCommentFragment : BaseFragment<FragmentDetailCommentBinding, RecomonViewModel>() {
	val adapter by lazy {
		CommonAdapter<CommentEntity>({ R.layout.comment to BR.comment })
	}
	private lateinit var commentEntity: CommentEntity
	val listOfCommentEntities by lazy {
		listOf(commentEntity)
	}
	lateinit var date: List<CommentEntity>
	var itemid = ""
	val list by lazy {
		listOf<Any>()
	}
	
	var isLoad = true
	override fun initView() {
		binding.recComment.adapter = adapter
		binding.btnComment.setOnClickListener {
			var c = binding.etComment.text.toString()
			itemid = date.map { it.itemid }.first()
			commentEntity = CommentEntity(
				0, c, "2024-06-28 16:44:32",
				0, 0, itemid, "", list, 0,
				0, ""
			)
			viewModel.intent.trySend(RecommonIntent.putComment(commentEntity))
			binding.etComment.text.clear()
		}
	}
	
	
	override fun initData() {
		if (isLoad) {
			viewModel.state.observe(this) {
				when (it) {
					is RecommonState.CommentSuccess -> {
						date = it.comment.data
						adapter += date
					}
					
					is RecommonState.RecommonFail -> {
						Toast.makeText(requireContext(), "${it.error}", Toast.LENGTH_SHORT).show()
					}
					
					is RecommonState.putComment -> {
						adapter += listOfCommentEntities
						Toast.makeText(requireContext(), "发送成功", Toast.LENGTH_SHORT).show()
					}
				}
			}
			val data = arguments?.getParcelable<SimpleVideoEntity>("data") as SimpleVideoEntity
			viewModel.intent.trySend(RecommonIntent.getComment(0, data.item_id.toString()))
			isLoad = false
		}
	}
}